//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Ftps {
    private static final Logger log = LoggerFactory.getLogger(Ftps.class);
    private String Control_Encoding = "UTF-8";
    private FTPSClient client = null;
    private String host = "";
    private int port = 21;
    private String user = "";
    private String password = "";
    private String ftpPath = "/";
    public static final String ANONYMOUS_USER = "anonymous";

    private Ftps() {
    }

    public Ftps(String host, int port, String user, String pwd) {
        this.host = host;
        this.port = port;
        this.user = user;
        this.password = pwd;
    }

    public String getHome() {
        return this.ftpPath;
    }

    public boolean connect() throws Exception {
        if (this.client == null) {
            this.client = new FTPSClient();
        }

        if (this.client.isConnected()) {
            return true;
        } else {
            this.client.setControlEncoding(this.Control_Encoding);

            try {
                this.client.connect(this.host, this.port);
                if (this.user != null && !"".equals(this.user)) {
                    this.client.login(this.user, this.password);
                } else {
                    this.client.login("anonymous", "anonymous");
                }

                this.client.setFileType(2);
                int reply = this.client.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply)) {
                    this.client.disconnect();
                    throw new Exception("connection FTP fail!");
                } else {
                    FTPClientConfig config = new FTPClientConfig(this.client.getSystemType().split(" ")[0]);
                    config.setServerLanguageCode("zh");
                    this.client.configure(config);
                    this.client.enterLocalPassiveMode();
                    this.client.setFileType(2);
                    this.client.setFileTransferMode(10);
                    return this.client.isConnected();
                }
            } catch (IOException var3) {
                throw new Exception("connection FTP fail! " + var3);
            }
        }
    }

    public void close() {
        if (this.client != null && this.client.isConnected()) {
            try {
                this.client.logout();
                this.client.disconnect();
            } catch (IOException var2) {
                var2.printStackTrace();
            }
        }

    }

    public List<FTPFile> list() {
        List<FTPFile> list = null;

        try {
            FTPFile[] ff = this.client.listFiles(this.ftpPath);
            if (ff != null && ff.length > 0) {
                list = new ArrayList(ff.length);
                Collections.addAll(list, ff);
            } else {
                list = new ArrayList(0);
            }
        } catch (IOException var3) {
            var3.printStackTrace();
        }

        return list;
    }

    public void switchDirectory(String path, boolean forcedIncrease) {
        try {
            if (path != null && !"".equals(path)) {
                boolean ok = this.client.changeWorkingDirectory(path);
                if (ok) {
                    this.ftpPath = path;
                } else if (forcedIncrease) {
                    StringTokenizer token = new StringTokenizer(path, "\\//");

                    while(token.hasMoreTokens()) {
                        String npath = token.nextToken();
                        this.client.makeDirectory(npath);
                        this.client.changeWorkingDirectory(npath);
                        if (ok) {
                            this.ftpPath = path;
                        }
                    }
                }
            }
        } catch (Exception var6) {
            var6.printStackTrace();
        }

    }

    public void createDirectory(String path) {
        try {
            if (path != null && !"".equals(path)) {
                boolean ok = this.client.changeWorkingDirectory(path);
                if (!ok) {
                    StringTokenizer token = new StringTokenizer(path, "\\//");

                    while(token.hasMoreTokens()) {
                        String npath = token.nextToken();
                        this.client.makeDirectory(npath);
                        this.client.changeWorkingDirectory(npath);
                    }
                }
            }

            this.client.changeWorkingDirectory(this.ftpPath);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

    }

    public boolean existFile(String path) {
        boolean flag = false;

        try {
            FTPFile[] ftpFileArr = this.client.listFiles(path);
            if (ftpFileArr.length > 0) {
                flag = true;
            }
        } catch (IOException var5) {
            var5.printStackTrace();
        }

        return flag;
    }

    public boolean deleteDirectory(String path) {
        boolean flag = false;

        try {
            flag = this.client.removeDirectory(path);
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return flag;
    }

    public boolean deleteFile(String path) {
        boolean flag = false;

        try {
            flag = this.client.deleteFile(path);
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return flag;
    }

    public boolean upload(File localFile) {
        return this.upload(localFile, "");
    }

    public boolean upload(File localFile, String reName) {
        boolean flag = false;
        String targetName = reName;
        if (reName == null || "".equals(reName)) {
            targetName = localFile.getName();
        }

        FileInputStream fis = null;

        try {
            fis = new FileInputStream(localFile);
            this.client.setControlEncoding(this.Control_Encoding);
            this.client.setFileType(2);
            boolean ok = this.client.storeFile(targetName, fis);
            if (ok) {
                flag = true;
            }
        } catch (IOException var7) {
            var7.printStackTrace();
        }

        return flag;
    }

    public boolean download(String ftpFileName, String savePath) {
        boolean flag = false;
        File dir = new File(savePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        FileOutputStream fos = null;

        try {
            String saveFile = dir.getAbsolutePath() + File.separator + ftpFileName;
            fos = new FileOutputStream(new File(saveFile));
            boolean ok = this.client.retrieveFile(ftpFileName, fos);
            if (ok) {
                flag = true;
            }
        } catch (IOException var8) {
            var8.printStackTrace();
        }

        return flag;
    }

    public void getFtpsDirList(List<Map<String, String>> list, String repoId, String baseDir) throws IOException {
        this.client.changeWorkingDirectory(baseDir);
        FTPFile[] files = this.client.listFiles();
        FTPFile[] var5 = files;
        int var6 = files.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            FTPFile file = var5[var7];
            HashMap dirMap;
            if (file.isFile()) {
                dirMap = new HashMap();
                dirMap.put("id", repoId + "@" + CodeUtil.getInstance().encode(baseDir + "/" + file.getName()));
                dirMap.put("pId", repoId + "@" + CodeUtil.getInstance().encode(baseDir));
                dirMap.put("name", file.getName());
                dirMap.put("path", baseDir + "/" + file.getName());
                if (StringTools.endsWith(file.getName(), "ktr")) {
                    dirMap.put("type", repoId + "@ftp@transformation");
                } else if (StringTools.endsWith(file.getName(), "kjb")) {
                    dirMap.put("type", repoId + "@ftp@job");
                }

                dirMap.put("lasted", "true");
                list.add(dirMap);
            } else if (file.isDirectory() && !".".equals(file.getName()) && !"..".equals(file.getName())) {
                dirMap = new HashMap();
                dirMap.put("id", repoId + "@" + CodeUtil.getInstance().encode(file.getName()));
                dirMap.put("pId", repoId + "@" + CodeUtil.getInstance().encode(baseDir));
                dirMap.put("name", file.getName());
                dirMap.put("type", "subTree");
                dirMap.put("path", baseDir + "/" + file.getName());
                dirMap.put("lasted", "false");
                list.add(dirMap);
                this.getFtpsDirList(list, repoId, dirMap.get("path") + "");
            }
        }

    }

    public static void main(String[] args) {
        Ftps ftp = new Ftps("127.0.0.1", 21, "myftp", "myftp@2020");

        try {
            ftp.connect();
            System.out.println("当前位置：" + ftp.getHome());
            ftp.switchDirectory("/test", true);
            System.out.println("当前位置：" + ftp.getHome());
            List<FTPFile> list = ftp.list();
            System.out.println("|-- " + ftp.getHome());
            Iterator var3 = list.iterator();

            while(var3.hasNext()) {
                FTPFile f = (FTPFile)var3.next();
                System.out.println(" |-- [" + (f.getType() == 0 ? "文件" : "文件夹") + "]" + f.getName());
            }

            boolean r1 = ftp.upload(new File("C:\\Users\\joymt\\Documents\\ftp\\测试文件1.txt"), "测试文件2.txt");
            System.out.println("上传文件：" + r1);
            boolean r2 = ftp.download("测试文件2.txt", "C:\\Users\\joymt\\Documents\\ftp");
            System.out.println("下载文件：" + r2);
            boolean r3 = ftp.deleteFile("/test/测试文件2.txt");
            System.out.println("删除文件：" + r3);
            boolean r4 = ftp.deleteDirectory("/test");
            System.out.println("删除目录：" + r4);
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        ftp.close();
    }
}
